import random
from fake_useragent import UserAgent
import requests
from bs4 import BeautifulSoup
import lxml
from lxml import etree
import re
import json
from chaojiying import crack_verification
from chaojidddd import parse_base64

"""
1.访问 城市列表  被重定向到  验证码
2.验证码 重定向： ext   LoO148=
3.为了获取验证码 发起请求   token: s48F8boj8Rz8NVrjqbrGDDKP8buzuczU
4. 预校验  pre-validate   token: s48F8boj8Rz8NVrjqbrGDDKP8buzuczU   challenger: 4987
5. 校验 validate    ext  LoO148=  token:"s48F8boj8Rz8NVrjqbrGDDKP8buzuczU"

"""


def get_token():
    token = ""
    m = "Rz94PnPbzNDiHpb28GcTjFFpg8Pm8DozBejeYqKrTc8RusEJ8YicVnRmEjUHDK7r"

    for i in range(0, 32):
        n = random.choice(m)
        token += n
    return token


ua = UserAgent().random
print(ua)

headers = {
    'User-Agent': ua,
}
token = get_token()
session = requests.Session()


def crack_verification(response):
    target = response.headers['location']
    location = re.search(r'location=(.*?)&(.*)', target).group(1)
    ext = re.search(r'location=(.*?)&ext=(.*)', target).group(2)
    print(location, ext)
    while True:
        resource_res = session.get(f"https://captcha.lianjia.com/captcha/resource?sceneId=sec-hip&token={token}",
                                   headers=headers)
        print(resource_res.json())
        base64 = resource_res.json()['data']['puzzle']['resource'][24:]
        print(base64)
        verification = parse_base64(base64)
        print(verification)
        pre_validate_res = session.get(
            f'https://captcha.lianjia.com/captcha/pre-validate?sceneId=sec-hip&token={token}&challenger={verification}',
            headers=headers)
        print(pre_validate_res.json())
        if pre_validate_res.json()['data']:
            break
    data = {
        "token": token,
        "scene": "sec-hip",
        "location": location,
        "ext": ext
    }
    print(session.cookies)
    validate_res = session.post("https://hip.lianjia.com/api/v1/validate", json=data, headers=headers)
    print(session.cookies)
    print(validate_res.status_code)


response = session.get('https://www.lianjia.com/city/', headers=headers, allow_redirects=False)
print(response.status_code)
if response.status_code == 302:
    crack_verification(response)  # 解析验证码
response_n = session.get("https://www.lianjia.com/city/", headers=headers, allow_redirects=False)
soup = BeautifulSoup(response_n.text, "lxml")
city_tags = soup.select(".city_list li a")
for city_tag in city_tags:
    print(city_tag.text, city_tag["href"])
